package com.sodaopen.study.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sodaopen.study.entity.AjaxResult;
import com.sodaopen.study.entity.ApiResult;
import com.sodaopen.study.entity.Course;
import com.sodaopen.study.entity.Stage;
import com.sodaopen.study.entity.Teacher;
import com.sodaopen.study.service.CourseService;
import com.sodaopen.study.service.StageService;
import com.sodaopen.study.utils.UploadHelper;

@Controller
public class CourseController {

    @Value("${web.upload-path}")
    private String filePath;

    @Autowired
    private CourseService courseService;
    
    @Autowired
    private StageService stageService;

    @GetMapping("content/course")
    public String course(){
        return "course";
    }

    @GetMapping("/api/course/list")
    @ResponseBody
    public Object listCourse(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "15") Integer limit){

        ApiResult result = new ApiResult();
        try{
            Page<Course> pageContent = PageHelper.startPage(page, limit);
            List<Course> list = courseService.findByParam();
            result.setCode(0);
            result.setCount(pageContent.getTotal());
            result.setData(list);
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("数据请求失败");
        }
        return result;
    }

    @GetMapping("content/course/add")
    public String courseAdd(Model model){
    	List<Stage> stageList = stageService.findAll();
    	model.addAttribute("stageList", stageList);
    	
        return "course-add";
    }

    @PostMapping("content/course/add")
    public String doAddCourse(MultipartFile file, Course course, Model model, HttpSession session){
        AjaxResult result = UploadHelper.saveFile(file, "f", filePath, false);
        if(result.isSuccess()) {
            course.setPic((String)result.getData());

            Teacher teacher = (Teacher)session.getAttribute("loginUser");
            course.setTeacherId(teacher.getId());

            if(course.getSort() == null){
                Integer maxSort = courseService.findMaxSort(course.getStageId());
                course.setSort(maxSort+5);
            }

            courseService.saveCourse(course);
            model.addAttribute("msg", "课程保存成功");
            
            List<Stage> stageList = stageService.findAll();
        	model.addAttribute("stageList", stageList);
        	
        }else {
            model.addAttribute("msg", "课程保存失败！");
        }

        return "course-add";
    }
}
